/**
 * 
 */
package com.inspur.cloud.demo.controller;

import com.inspur.cloud.demo.entity.FlinkConfig;
import com.inspur.cloud.demo.service.flinksql.FlinkSqlService;
import com.inspur.cloud.demo.util.Response;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sql")
@Api(tags="实时计算Sql接口")
public class flinkSqlController {
    @Autowired
    FlinkSqlService flinkSqlService;
    /*private final FlinkRespository flinkRespository;
    private final FlinkSqlService flinkSqlService;

    flinkSqlController(FlinkRespository flinkRespository,FlinkSqlService flinkSqlService)
    {
        this.flinkRespository = flinkRespository;
        this.flinkSqlService = flinkSqlService;
    }*/

    @GetMapping("/getFlinkConfig")
    @ApiOperation(value = "获取配置信息", notes = "根据给定的sql语句运行sql")
    public FlinkConfig getFlinkConfig() {
        return flinkSqlService.getFlinkConfig();
    }

    @PostMapping("/runSql")
    @ApiOperation(value = "运行flinkSql语句", notes = "根据给定的flinkSql语句运行任务 包括创建表 查询结果等 如果type 为sql,那么是一个动态sql")
    @ApiImplicitParam(name = "paramInfo" ,dataType="Map", paramType = "body",value="{\"sql\":\"select a.access,b.data from cdn_access_statistic a ,cdn_access_data b where a.access = b.access;\",\"type\":\"dymac\",\"notebookid\":\"noteid\",\"paragraphsid\":\"paragrahid\"}",examples = @Example({
            @ExampleProperty(value = "{'sql':'sql','type':'sql','notebookid':'noteid','paragraphsid':'paragh'}")
    }))
    //@ApiImplicitParams(
    //        value = { @ApiImplicitParam( name = "sql",value = "sql语句",dataType = "Map", paramType = "body")})
    //@ApiImplicitParam(name = "paramInfo", value = "{tableParams:\\{mdbCode:123},appParams:\\{mdbCode:123/},databaseParams:\\{mdbCode:123/},page:1,pageSize:10/}类型的参数，为完全匹配查询", required = true, dataType = "Map", paramType = "body")
    //@ApiImplicitParams(
    //        value = { @ApiImplicitParam( name = "sql",value = "查询条件",dataType = "string", paramType = "body")})
    public Response<?> runSql(@NotNull(message = "语句不能为空") @RequestBody Map paramInfo) throws Exception {
        //首先查询默认的动态任务是否已经创建 如果创建成功才可以运行动态语句
        HashMap<String,Object> param  = new HashMap<>();
        HashMap<String,Object> params  = new HashMap<>();
        String type = (String)paramInfo.get("type");

        if(type!=null&&type.equals("sql"))
        {
            params.put("sql",paramInfo.get("sql"));//sql为固定格式
            param.put("params",params);
            return flinkSqlService.runSql(param);
        }else
        {
            String notebookid = (String)paramInfo.get("notebookid");//
            String paragraphsid = (String)paramInfo.get("paragraphsid");//
            String sql = (String)paramInfo.get("sql");
            if(sql!=null&& !sql.equals("")) {
                params.put("sql", paramInfo.get("sql"));//sql为固定格式  //后期
            }
            param.put("params",params);
            return flinkSqlService.runSql(notebookid,paragraphsid,param);
        }
    }


    @PostMapping("/createDynamicSql")
    @ApiOperation(value = "创建动态Sql执行器", notes = "创建默认执行器")
    public  Response<?> createDynamicSql() throws Exception {
        //首先查询默认的动态任务是否已经创建 如果创建成功才可以运行动态语句
        return flinkSqlService.createDynamicSql();
    }

    @GetMapping("/queryGroupJobSql")
    @ApiOperation(value = "获取所有节点组信息", notes = "查询节点组信息")
    public Response<?>  queryGroupJobSql() {

        return flinkSqlService.queryGroupJobSql();
    }

    @GetMapping("/querySubGroupJobSql")
    @ApiOperation(value = "获取某个节点子任务信息", notes = "查询某个节点子任务信息")
    @ApiImplicitParam(name = "id" ,dataType="Map", paramType = "query",value="{\"2FCSNDZWW\"}",examples = @Example({
            @ExampleProperty(value = "{'id'}")
    }))
    public Response<?>  querySubGroupJobSql(@NotNull(message = "语句不能为空") @RequestParam Map id) {
        return flinkSqlService.querySubGroupJobSql(id);
    }



}
